package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.Travel;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.search.domian.DestinationEs;
import cn.wolfcode.luowowo.search.domian.StrategyEs;
import cn.wolfcode.luowowo.search.domian.TravelEs;
import cn.wolfcode.luowowo.search.domian.UserInfoEs;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.ISearchService;
import cn.wolfcode.luowowo.search.vo.SearchResult;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.ITravelService;
import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
public class SearchController {


    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IUserInfoService userInfoService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ISearchService searchService;

    @GetMapping("/q")
    public Object search(SearchQueryObject qo) throws Exception {

        // 解码器
        String decode = URLDecoder.decode(qo.getKeyword(), "UTF-8");
        qo.setKeyword(decode);

        switch (qo.getType()){
            case SearchQueryObject.TYPE_DEST:
                return SearchDest(qo);
            case SearchQueryObject.TYPE_STRATEGY:
                return SearchStrategy(qo);
            case SearchQueryObject.TYPE_TRAVEL:
                return SearchTravel(qo);
            case SearchQueryObject.TYPE_USER:
                return SearchUser(qo);
            default:return SearchAll(qo);
        }

    }


    // 目的地
    private Object SearchDest(SearchQueryObject qo){
        HashMap map = new HashMap();

        SearchResult searchResult = new SearchResult();
        Destination dest= destinationService.findByName(qo.getKeyword());
        if (dest != null){
        List<UserInfo> us =  userInfoService.findByCity(dest.getName());
        List<Travel> ts =  travelService.findByDestName(dest.getName());
        List<Strategy> sts =  strategyService.findByDestName(dest.getName());
        searchResult.setStrategys(sts);
        searchResult.setTravels(ts);
        searchResult.setUsers(us);
        searchResult.setTotal(us.size() + ts.size() + sts.size() + 0L);
        }
        map.put("qo",qo);
        map.put("dest",dest);
        map.put("result",searchResult);

        return JsonResult.success(map);
    }

    // 用户
    private Object SearchUser(SearchQueryObject qo){
        Page<UserInfo> page = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME, UserInfo.class, qo, "info","nickname");
        Map map = new HashMap();
        map.put("qo",qo);
        map.put("page",page);
        return JsonResult.success(map);
    }

    // 攻略
    private Object SearchStrategy(SearchQueryObject qo){
        Page<Strategy> page = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME, Strategy.class, qo, "title", "subTitle", "summary");
        Map map = new HashMap();
        map.put("qo",qo);
        map.put("page",page);
        return JsonResult.success(map);
    }

    // 游记
    private Object SearchTravel(SearchQueryObject qo){
        Page<Travel> page = searchService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME,Travel.class, qo, "title","summary");
        Map map = new HashMap();
        map.put("qo",qo);
        map.put("page",page);
        return JsonResult.success(map);

    }

    // 全部
    private Object SearchAll(SearchQueryObject qo){
        SearchResult result = new SearchResult();
        Page<UserInfo> us = searchService.searchWithHighlight(UserInfoEs.INDEX_NAME, UserInfoEs.TYPE_NAME, UserInfo.class, qo, "info","nickname");
        Page<Travel> ts = searchService.searchWithHighlight(TravelEs.INDEX_NAME, TravelEs.TYPE_NAME,Travel.class, qo, "title","summary");
        Page<Strategy> sts = searchService.searchWithHighlight(StrategyEs.INDEX_NAME, StrategyEs.TYPE_NAME, Strategy.class, qo, "title", "subTitle", "summary");
        Page<Destination> ds = searchService.searchWithHighlight(DestinationEs.INDEX_NAME, DestinationEs.TYPE_NAME, Destination.class, qo, "info");
        result.setTravels(ts.getContent());
        result.setStrategys(sts.getContent());
        result.setDests(ds.getContent());
        result.setUsers(us.getContent());
        result.setTotal(ts.getTotalElements() + us.getTotalElements() + sts.getTotalElements() + ds.getTotalElements());
        Map map = new HashMap();
        map.put("qo",qo);
        map.put("result",result);

        return JsonResult.success(map);

    }}
